﻿/*******************************************************************************
 * Copyright © 2017 FriendStar.Framework 版权所有
 * Author: HYF
 * Description: FriendStar快速开发平台
 * Website：http://www.FriendStar.cn
*********************************************************************************/
using FriendStar.Application.SystemManage;
using FriendStar.Application.MainData;
using FriendStar.Code;
using FriendStar.Domain.Entity.BaseData;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using FriendStar.Domain.Entity.MainData;

namespace FriendStar.Web.Areas.MainData.Controllers
{
    public class ItemsTypeController : ControllerBase
    {
        BUS_SJ_DictionaryClassApp bus_sj_dictionaryclassApp = new BUS_SJ_DictionaryClassApp();

        [HttpGet]
        [HandlerAjaxOnly]
        public ActionResult GetTreeSelectJson()
        {
            var data = bus_sj_dictionaryclassApp.GetList();
            var treeList = new List<TreeSelectModel>();
            foreach (var item in data)
            {
                TreeSelectModel treeModel = new TreeSelectModel();
                treeModel.id = item.FClassID;
                treeModel.text = item.FClassName;
                treeModel.parentId = item.FParentId;
                treeList.Add(treeModel);
            }
            return Content(treeList.TreeSelectJson());
        }
        [HttpGet]
        [HandlerAjaxOnly]
        public ActionResult GetTreeJson()
        {
            var data = bus_sj_dictionaryclassApp.GetList();
            var treeList = new List<TreeViewModel>();
            foreach (var item in data)
            {
                TreeViewModel tree = new TreeViewModel();
                bool hasChildren = data.Count(t => t.FParentId == item.FClassID) == 0 ? false : true;
                tree.id = item.FClassID;
                tree.text = item.FClassName;
                tree.value = item.FClassNO;
                tree.parentId = string.IsNullOrEmpty(item.FParentId) ? "0" : item.FParentId;
                tree.isexpand = true;
                tree.complete = true;
                tree.hasChildren = hasChildren;
                treeList.Add(tree);
            }
            return Content(treeList.TreeViewJson());
        }
        [HttpGet]
        [HandlerAjaxOnly]
        public ActionResult GetTreeGridJson()
        {
            var data = bus_sj_dictionaryclassApp.GetList();
            var treeList = new List<TreeGridModel>();
            foreach (var item in data)
            {
                TreeGridModel treeModel = new TreeGridModel();
                bool hasChildren = data.Count(t => t.FParentId == item.FClassID) == 0 ? false : true;
                treeModel.id = item.FClassID;
                treeModel.isLeaf = hasChildren;
                treeModel.parentId = string.IsNullOrEmpty(item.FParentId) ? "0" : item.FParentId; ;
                treeModel.expanded = hasChildren;
                treeModel.entityJson = item.ToJson();
                treeList.Add(treeModel);
            }
            return Content(treeList.TreeGridJson());
        }
        [HttpGet]
        [HandlerAjaxOnly]
        public ActionResult GetFormJson(string keyValue)
        {
            var data = bus_sj_dictionaryclassApp.GetForm(keyValue);
            return Content(data.ToJson());
        }
        [HttpPost]
        [HandlerAjaxOnly]
        [ValidateAntiForgeryToken]
        public ActionResult SubmitForm(BUS_SJ_DictionaryClassEntity entity, string keyValue)
        {
            if(bus_sj_dictionaryclassApp.CheckedEntity(entity))
            {
                bus_sj_dictionaryclassApp.SubmitForm(entity, keyValue);
                return Success("操作成功。");
            }
            else
            {
                return Error("操作失败，已经存在相同字典分类编号。");
            }
        }
        [HttpPost]
        [HandlerAjaxOnly]
        [ValidateAntiForgeryToken]
        public ActionResult DeleteForm(string keyValue)
        {
            bus_sj_dictionaryclassApp.DeleteForm(keyValue);
            return Success("删除成功。");
        }
    }
}
